/**
 * Copyright 2010 Victor Egea Hernando
 *
 * RockScript is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * RockScript is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with RockScript.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "paper2lir.h"
#include "vmachine.h"

namespace paper{

paper2lir &paper2lir::translate( paper::vmachine &vm, paper::method &method, pjit::pjit_buffer &buffer ){
	// add main entry label & get VM pointer
	corelib::uint32 main_entry = buffer.next_label();
	void *vmachine_ptr         = reinterpret_cast<void*>( &vm );
	void *vmaroutine_ptr       = reinterpret_cast<void*>( &vm._aroutine );
	void *op_return_ptr        = reinterpret_cast<void*>( &vmachine::op_return );
	void *op_cppcode_ptr       = reinterpret_cast<void*>( &vmachine::op_cppcode );
	// commit main entry
	buffer.lir_label( main_entry );
	// translate VM opcodes
	for(corelib::uint32 idx = 0;idx<method._opcode.size();idx++){
		// get opcode
		instruction &ci = method._opcode[idx];
		// interpret opcode
		switch ( ci._inst._op ) {
			// LOADING/SAVING OPCODES ---------------------------------------------
			case paper::OP_MOVE:{
				break;
			}case paper::OP_SAVE:{
				break;
			}case paper::OP_LOAD:{
				break;
			}case paper::OP_LOADCK:{
				break;
			}case paper::OP_LOADMK:{
				break;
			}case paper::OP_LOADIA:{
				break;
			}case paper::OP_LOADIZ:{
				break;
			// ARITHMETICAL INTEGER OPCODES --------------------------------------------
			}case paper::OP_ADDI:{
				break;
			}case paper::OP_SUBI:{
				break;
			}case paper::OP_MULI:{
				break;
			}case paper::OP_DIVI:{
				break;
			}case paper::OP_MODI:{
				break;
			}case paper::OP_BWANDI:{
				break;
			}case paper::OP_BWORI:{
				break;
			}case paper::OP_BWXORI:{
				break;
			}case paper::OP_BWSHLI:{
				break;
			}case paper::OP_BWSHRI:{
				break;
			}case paper::OP_BWUSHRI:{
				break;
			}case paper::OP_ABSI:{
				break;
			}case paper::OP_BWNOTI:{
				break;
			}case paper::OP_INCI:{
				break;
			}case paper::OP_DECI:{
				break;
			// CONDITIONAL OPCODES ------------------------------------------------
			}case paper::OP_RCJMP:{
				break;
			}case paper::OP_RJMP:{
				break;
			}case paper::OP_NOT:{
				break;
			}case paper::OP_EQI:{
				break;
			}case paper::OP_EQR:{
				break;
			}case paper::OP_NEQI:{
				break;
			}case paper::OP_NEQR:{
				break;
			}case paper::OP_LTI:{
				break;
			}case paper::OP_LTR:{
				break;
			}case paper::OP_LEQI:{
				break;
			}case paper::OP_LEQR:{
				break;
			}case paper::OP_GTI:{
				break;
			}case paper::OP_GTR:{
				break;
			}case paper::OP_GEI:{
				break;
			}case paper::OP_GER:{
				break;
			// METHOD OPCODES ----------------------------------------------------------
			}case paper::OP_LOADARG:{
				break;
			}case paper::OP_KCCALL:{
				break;
			}case paper::OP_UCCALL:{
				continue;
			}case paper::OP_CPPCODE:{
				// forbidden!!
				break;
			}case paper::OP_RETURN:{
				// call vmachine::op_return
				buffer.lir_pushargregref( 0 );
				buffer.lir_pushargptr( vmachine_ptr );
				buffer.lir_call( op_return_ptr );
				buffer.lir_ret();
				break;
			// OBJECT OPCODES -----------------------------------------------------------
			}case paper::OP_NEW:{
				break;
			}case paper::OP_DELETE:{
				break;
			}
		}
	}
}

}; // namespace paper
